package DBLayer;

import ModelLayer.*;
import java.sql.*;
import java.util.ArrayList;

public class DBChild implements DBChildIF
{
	private Connection con;
    
    public DBChild() 
    {
      con = DBConnection.getInstance().getDBcon();
    }
    
    public ArrayList<Child> getAllChilds(boolean retriveAssociation)
    {
        return miscWhere("", retriveAssociation);
    }

    public Child searchChildID(int ChildID, boolean retriveAssociation)
    {   
    	String wClause = "ChildID = " + ChildID + "";
        return singleWhere(wClause, retriveAssociation);
    }
	
	private ArrayList<Child> miscWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		ArrayList<Child> ChildList = new ArrayList<Child>();	
		
		String query =  buildQuery(wClause);
  
  		try
  		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
			while( results.next() )
			{
				Child cObj = new Child();
				cObj = buildChild(results);
				ChildList.add(cObj);
			}
			stmt.close();
		}
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception - select: "+e);
			e.printStackTrace();
	 	}
		return ChildList;
	}
	
	private Child singleWhere(String wClause, boolean retrieveAssociation)
	{
		ResultSet results;
		Child cObj = new Child();
	    String query =  buildQuery(wClause);
		try
		{
	 		Statement stmt = con.createStatement();
	 		stmt.setQueryTimeout(5);
	 		results = stmt.executeQuery(query);
	 		
	 		if( results.next() )
	 		{
	 			cObj = buildChild(results);
	 			stmt.close();
			}
		}	
	 	catch(Exception e)
	 	{
	 		System.out.println("Query exception: "+e);
	 	}
		return cObj;
	}

	private String buildQuery(String wClause)
	{
	    String query="SELECT c.ChildID, c.CPR, c.registrationDate, p.PersonID, p.FirstName, p.LastName, p.Address, p.PostalCode, p.City, p.PhoneNr, p.MobileNr, p.Email FROM Child c, Person p";
	    query=query+" WHERE p.PersonID = c.PersonID";
		if (wClause.length()>0)
			query=query+" AND " + wClause;	
		return query;
	}

	private Child buildChild(ResultSet results)
    {   
		Child cObj = new Child();
          try
          {
                cObj.setChildID(results.getInt("ChildID"));
                cObj.setCPR(results.getString("CPR"));
                cObj.setRegistrationDate(results.getString("registrationDate"));
                cObj.setPersonID(results.getInt("PersonID"));
                cObj.setfName(results.getString("FirstName"));
                cObj.setlName(results.getString("LastName"));
                cObj.setAddress(results.getString("Address"));
                cObj.setPostalCode(results.getInt("PostalCode"));
                cObj.setCity(results.getString("City"));
                cObj.setPhoneNr(results.getString("PhoneNr"));
                cObj.setMobileNr(results.getString("MobileNr"));
                cObj.setEmail(results.getString("Email"));
          }
         catch(Exception e)
         {
             System.out.println("error in building the Child object");
         }
         return cObj;
      }

}